package org.spring.boot.sample.hystrix.command;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

public class HystrixCommandBasic extends HystrixCommand<String> {

	private final String name;
	
	public HystrixCommandBasic(String name){
		super(HystrixCommandGroupKey.Factory.asKey("MyHystrixGroup-A"));
        this.name = name;
	}

	@Override
	protected String run() throws Exception {
		// Thread.sleep(20000); run()方法执行超时时，会触发getFallback()方法的执行
		return "Hello " + name + "!";
	}

	public String getName() {
		return name;
	}

	/**
	 * 当抛出HystrixBadRequestException时，调用程序可以捕获异常，没有触发getFallback()，
	 * 而其他异常则会触发getFallback()，调用程序将获得getFallback()的返回
	 */
	@Override
	protected String getFallback() {
		System.out.println("Hello " + name + " Fallback!");
		return "Hello " + name + " Fallback!";
	}

}
